package cn.darksoul3.springcloud.alibaba.contorller;

import cn.darksoul3.common.result.ObjectResult;
import cn.darksoul3.springcloud.alibaba.blcok.CustomerBlockHandler;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;

@Slf4j
@RestController
public class tick {

    @GetMapping("/tick")
    public ObjectResult nacosTick(){
        log.info("this is nacos tick");
        return ObjectResult.succeed(Collections.emptyList());
    }

    @GetMapping("/limitbyres")
    @SentinelResource(value = "limitbyres" ,  blockHandler = "handler")
    public ObjectResult nacosRateLimitByRes(){
        log.info("this is nacos rate limit by res");
        return ObjectResult.succeed(Collections.emptyList());
    }


    public ObjectResult handler(BlockException block){
        log.info("this is nacos block handler");
        return ObjectResult.fail(504,block.getClass().getCanonicalName());
    }


    @GetMapping("/limitbyurl")
    @SentinelResource(value = "limitbyurl")
    public ObjectResult nacosRateLimitByUrl(){
        log.info("this is nacos rate limit by url");
        return ObjectResult.succeed(Collections.emptyList());
    }

    @GetMapping("/limitbyself")
    @SentinelResource(value = "limitbyself",blockHandlerClass = CustomerBlockHandler.class,blockHandler = "handler")
    public ObjectResult nacosRateLimitBySelf(){
        log.info("this is nacos rate limit by self");
        return ObjectResult.succeed(Collections.emptyList());
    }
}
